package com.dartfar.workflow.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dartfar.base.action.BaseAction;
import com.dartfar.util.EntityUtils;
import com.dartfar.util.StringUtils;
import com.dartfar.workflow.constant.WorkflowCaleType;
import com.dartfar.workflow.model.WorkflowDeployTask;
import com.dartfar.workflow.model.WorkflowTaskFunMount;
import com.dartfar.workflow.service.WorkflowDeployTaskService;
import com.dartfar.workflow.vo.WorkflowDeployTaskVo;
import com.dartfar.workflow.vo.WorkflowEntrustVo;
import com.dartfar.workflow.vo.WorkflowTaskFunMountVo;

/**
 * 流程部署任务 节点委托人管理Action
 * @author 王琦
 *
 */
@Scope("prototype")
@Component("workflowDeployTaskAction")
public class WorkflowDeployTaskAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static Logger logger = Logger.getLogger(WorkflowDeployTaskAction.class);
	
	@Resource
	private WorkflowDeployTaskService workflowDeployTaskService;
	
	
	private int id;;
	private String taskName;
	
	private String updated;


	/**
	 * 保存任务节点信息
	 * @return
	 */
	public String save(){
		List<WorkflowDeployTaskVo> list = null;
		//更新键值
		logger.info("updated: " + updated);
		if(updated != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(updated, WorkflowDeployTaskVo.class);
			for(Iterator<WorkflowDeployTaskVo> w =  list.iterator(); w.hasNext();){
				WorkflowDeployTaskVo wdtVo = w.next();
				edit(wdtVo);
			}
		}
		logger.info("执行完毕");
		return SUCCESS;
	}
	
	
	public void edit(WorkflowDeployTaskVo wdtVo){
		WorkflowDeployTask wdt = workflowDeployTaskService.get(wdtVo.getId());
		try {
			//拷贝
			BeanUtils.copyProperties(wdt, wdtVo);
		}catch(Exception e){
			e.printStackTrace();
		}
		workflowDeployTaskService.update(wdt);
	}
	
	/**
	 * 加载任务委托人数据
	 * @return
	 */
	public void loadWorkflowDeployTaskEntrustData(){
		logger.info("id: " + id);
		logger.info("taskName: " + taskName);
		try{
		List<WorkflowDeployTaskVo> workflowDeployTaskVos = new ArrayList<WorkflowDeployTaskVo>();
		
		List<WorkflowDeployTask> list = workflowDeployTaskService.getEntrustManagerByTaskNode(id, taskName);
		
		for(Iterator<WorkflowDeployTask> w =  list.iterator(); w.hasNext();){
			WorkflowDeployTask wdt = w.next();
			WorkflowDeployTaskVo wdtVo = new WorkflowDeployTaskVo();
			//拷贝
			BeanUtils.copyProperties(wdtVo, wdt);
			//设置委托类型名称
			if(EntityUtils.isNotNull(wdtVo.getAssigneType())){
				wdtVo.setAssigneType(WorkflowCaleType.getCaleTypeName(wdtVo.getAssigneType()));
			}
			workflowDeployTaskVos.add(wdtVo);
		}
		
		String jsonResult = JSONArray.fromObject(workflowDeployTaskVos).toString();
		
		logger.info("jsonResult : " + jsonResult);
		
		
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
	/**
	 * 加载委托方式数据
	 */
	public void loadWorkflowEntrustData(){
		
		List<WorkflowEntrustVo> workflowEntrustVos = new ArrayList<WorkflowEntrustVo>();
		WorkflowEntrustVo wev1 = new WorkflowEntrustVo();
		wev1.setId("ASSIGNE");
		wev1.setName("执行人");
		workflowEntrustVos.add(wev1);
		
		WorkflowEntrustVo wev2 = new WorkflowEntrustVo();
		wev2.setId("USER");
		wev2.setName("候选人");
		workflowEntrustVos.add(wev2);
		
		WorkflowEntrustVo wev3 = new WorkflowEntrustVo();
		wev3.setId("ROLE");
		wev3.setName("角色");
		workflowEntrustVos.add(wev3);
		
		WorkflowEntrustVo wev4 = new WorkflowEntrustVo();
		wev4.setId("END");
		wev4.setName("归档");
		workflowEntrustVos.add(wev4);
		
		logger.info("workflowEntrustVos: " + workflowEntrustVos.size());
		String jsonResult = JSONArray.fromObject(workflowEntrustVos).toString();
		logger.info(jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}	
	}


	/**
	 * Getter And Setter
	 */
	public String getTaskName() {
		return taskName;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}
	public String getUpdated() {
		return updated;
	}
	public void setUpdated(String updated) {
		this.updated = updated;
	}
	
}
